﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;
using NW;

namespace PM_Remote
{
	public class PM_Client : NWClient
	{
		public PM_Client(string host, int port)
			: base(host, port)
		{
		}
		protected override bool _ProcessMessage()
		{
			PM_Instr I = (PM_Instr)FetchInt32();
			switch (I)
			{
				case PM_Instr.OpenProcess:
					int H = FetchInt32();
					Console.WriteLine("CLI: Handle received: " + H);
					break;
				case PM_Instr.ReturnBytes:
					int N = FetchInt32();
					result = FetchArray(N);
					Console.WriteLine("Array received: " + BitConverter.ToString(result));
					requestprocessed = true;
					break;
				default:
					Console.WriteLine("Unknown Code");
					break;
			}
			return true;
		}
		public void OpenProcess(String proc)
		{
			SendPacket((int)PM_Instr.OpenProcess, proc);
		}
		void SendByteArrayRequest(int A, int L)
		{
			SendPacket((int)PM_Instr.ReadBytes, A, L);
		}
		bool requestprocessed = false;
		byte[] result = null;
		public byte[] RequestData(int A, int L)
		{
			requestprocessed = false;
			SendByteArrayRequest(A, L);
			while (!requestprocessed)
				Thread.Sleep(10);
			return result;
		}
	}
}